package com.campus.delivery.config;

import com.campus.delivery.handler.NativeWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 注册原生WebSocket处理器，支持uni-app小程序
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private NativeWebSocketHandler nativeWebSocketHandler;

    /**
     * 注册原生WebSocket处理器
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册原生WebSocket处理器，支持uni-app小程序
        registry.addHandler(nativeWebSocketHandler, "/ws")
                .setAllowedOriginPatterns("*");
    }
}